package svgedit.gui.actions;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.AbstractAction;

import svgedit.gui.Frame;
import svgedit.gui.View;
import svgedit.gui.actions.ViewListener.ShapeType;
import svgedit.svg.SVGElement;
import svgedit.svg.SVGGroup;

/**
 * Action to group selected SVGElements into a SVGGroup elements.
 * The elements to be grouped are all listed in the SelectedElements in the view
 * @see svgedit.svg.SVGGroup
 * @see svgedit.svg.SVGElement
 * @see svgedit.gui.actions.SelectedElements
 * @see svgedit.gui.View
 */
public class GroupAction extends AbstractAction implements ActionListener
{
	private static final long serialVersionUID = -1947794846292648384L;

	public GroupAction()
	{
		super("Group");
	}

	/**
	 * Here we get the elements currently selected from SelectedElements and
	 * group them all together using the SVGGroup(ArrayList<SVGElements>) 
	 * constructor. This action just passes all the elements to that.
	 * @see svgedit.svg.SVGGroup
	 */
	@Override
	public void actionPerformed(ActionEvent e)
	{
		Frame frame = Frame.getFrame();
		View view = frame.getView();

		// set the tool to group - so 
		view.getViewListener().setShapeType(ShapeType.GROUP);

		ArrayList<SVGElement> elements = frame.getView().getSelected()
				.getList();
		System.out.println("Grouping " + elements.size() + " elements");

		SVGGroup group = new SVGGroup(elements);

		view.getSelected().clear();
		frame.enableGroupMenuItem(false);
		frame.enableUnGroupMenuItem(true);
		view.getSelected().add(group);
		view.repaint();
		
		// debug print
		//view.getDocument().getRootGroup().write(5);

		frame.modifiedTitle(true);
		view.getViewListener().setShapeType(ShapeType.NONE);
	}
}
